{% extends "base.html" %}

{% block title %}图书分类管理 - 墨韵图书共享平台{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card ink-card">
                <div class="card-header ink-header d-flex justify-content-between align-items-center">
                    <h4 class="mb-0"><i class="fas fa-tags me-2"></i>图书分类管理</h4>
                    <a href="{{ url_for('create_category') }}" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>创建新分类
                    </a>
                </div>
                <div class="card-body">
                    {% if categories %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>分类名称</th>
                                    <th>描述</th>
                                    <th>图书数量</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for category in categories %}
                                <tr>
                                    <td>{{ category.name }}</td>
                                    <td>{{ category.description or '暂无描述' }}</td>
                                    <td>{{ category.books|length }}</td>
                                    <td>
                                        <button class="btn btn-sm btn-outline-danger" 
                                                onclick="deleteCategory('{{ category.id }}')">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <p class="text-muted mb-0">暂无图书分类，请点击右上角创建</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.ink-card {
    border: none;
    border-radius: 10px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.ink-header {
    background-color: #f8f9fa;
    border-bottom: none;
    padding: 1.5rem;
}
</style>

<script>
function deleteCategory(categoryId) {
    if (confirm('确定要删除这个分类吗？如果分类下有图书将无法删除。')) {
        fetch(`/category/${categoryId}/delete`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert('删除失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('删除失败，请稍后重试');
        });
    }
}
</script>
{% endblock %} 